<meta charset="UTF-8">
<?php
include "../conn.php";
include "s-cookie.php";
$ProjectName = $_GET['txtTaskName'];
$ddlType = $_GET['ddlType'];
$ddlSource = $_GET['ddlSource'];
$txtTaskDescription = $_GET['txtTaskDescription'];
$StudentId = $_SESSION['s-id'];
$year = $_SESSION['s-Year'];

$pid = $_POST['projectid'];
$asstask = $_POST['asstask'];
$draft = $_POST['draft'];

if($ddlType!=""&&$ddlSource!="") {
    $sql = "select max(ProjectId) from probyself ";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_row($result);
    $ProjectId = $row[0] + 1;
    $ProjectId;
    $sql = "insert into probyself (ProjectId,ProjectName,Year,Category,ProjectOrigin,Summary,StudentId)VALUES ('$ProjectId','$ProjectName','$year','$ddlType','$ddlSource', '$txtTaskDescription','$StudentId')";

    if (mysqli_query($conn, $sql)) {
        ?>

        <script type="text/javascript">
            alert("题目自拟成功");
        </script>
        <?
        echo "<script language=\"javascript\">location.href='teachresearch.php';</script>";
        ?>
        <script type="text/javascript">
            alert("题目自拟失败");
        </script>
        <?
        echo "<script language=\"javascript\">location.href='teachresearch.php';</script>";
    }
}

//上传任务书

if ($asstask != null) {
    if (!empty($_FILES['upassfile']['name'])) {
        $fileinfo = $_FILES['upassfile'];
        $type = strstr($fileinfo['name'], ".");
        if ($type != ".docx") {
            ?>
            <script type="text/javascript">
                alert("请上传docx格式文件！!");
            </script>
            <?
            echo "<script language=\"javascript\">location.href='s-mykt.php';</script>";
        } else {
            if ($fileinfo['size'] < 2097152 && $fileinfo['size'] > 0) {

                $name = $fileinfo['name'];
                $name = iconv("UTF-8", "gb2312", $name);
                $path2 = "../uptaskfile/" . $_FILES['upassfile']['name'];
                $path = "../uptaskfile/" . $name;
                if (move_uploaded_file($fileinfo['tmp_name'], $path)) {
                    $updatesql = "update project set Assignment='$path2' WHERE ProjectId=$pid";
                    if ($conn->query($updatesql)) {
                        ?>
                        <script type="text/javascript">
                            alert("上传成功!");
                        </script>
                        <?
                        echo "<script language=\"javascript\">location.href='s-mykt.php';</script>";
                    } else {
                        ?>
                        <script type="text/javascript">
                            alert("上传失败!");
                        </script>
                        <?
                        echo "<script language=\"javascript\">location.href='s-mykt.php';</script>";
                    }
                }
                if (is_dir("../uptaskfile/")) {
                    $dir = scandir("../uptaskfile/");
                    foreach ($dir as $value) {
                        echo $value;
                    }
                } else {
                    ?>
                    <script type="text/javascript">
                        alert("目录路径错误!");
                    </script>
                    <?
                    echo "<script language=\"javascript\">location.href='s-mykt.php';</script>";
                }
            } else {
                ?>
                <script type="text/javascript">
                    alert("请传输文件小于2M的文件");
                </script>
                <?
                echo "<script language=\"javascript\">location.href='s-mykt.php';</script>";
            }
        }
    } else {
        ?>
        <script type="text/javascript">
            alert("未发现文件");
        </script>
        <?
        echo "<script language=\"javascript\">location.href='s-mykt.php';</script>";
    }
}
//上传答辩稿
if ($draft != null) {
    if (!empty($_FILES['uptaskfile']['name'])) {
        $fileinfo = $_FILES['uptaskfile'];
        $type = strstr($fileinfo['name'], ".");
        if ($type != ".docx") {
            ?>
            <script type="text/javascript">
                alert("请上传docx格式文件！!");
            </script>
            <?
            echo "<script language=\"javascript\">location.href='s-mykt.php';</script>";

        } else {
            if ($fileinfo['size'] < 2097152 && $fileinfo['size'] > 0) {

                $name = $fileinfo['name'];
                $name = iconv("UTF-8", "gb2312", $name);
                $path2 = "../updraftfile/" . $_FILES['uptaskfile']['name'];
                $path = "../updraftfile/" . $name;
                if (move_uploaded_file($fileinfo['tmp_name'], $path)) {
                    $updatesql = "update project set Draft='$path2' WHERE ProjectId=$pid";
                    if ($conn->query($updatesql)) {
                        ?>
                        <script type="text/javascript">
                            alert("上传成功!");
                        </script>
                        <?
                        echo "<script language=\"javascript\">location.href='s-mykt.php';</script>";
                    } else {
                        ?>
                        <script type="text/javascript">
                            alert("上传失败!");
                        </script>
                        <?
                        echo "<script language=\"javascript\">location.href='s-mykt.php';</script>";
                    }
                }
                if (is_dir("../uptaskfile/")) {
                    $dir = scandir("../uptaskfile/");
                    foreach ($dir as $value) {
                        echo $value;
                    }
                } else {
                    ?>
                    <script type="text/javascript">
                        alert("目录路径错误!");
                    </script>
                    <?
                    echo "<script language=\"javascript\">location.href='s-mykt.php';</script>";
                }
            } else {
                ?>
                <script type="text/javascript">
                    alert("请传输文件小于2M的文件");
                </script>
                <?
                echo "<script language=\"javascript\">location.href='s-mykt.php';</script>";
            }
        }
    }else {
        ?>
        <script type="text/javascript">
            alert("未发现文件");
        </script>
        <?
        echo "<script language=\"javascript\">location.href='s-mykt.php';</script>";
}
}
?>
